package com.luchao.back.common.config;


import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.common.exception.CheckIntegralException;
import com.luchao.back.common.exception.OrderShoesNullPointException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ResponseBody
    //其他未知异常
    @ExceptionHandler(Exception.class)
    public ResponseResult<String> otherExcptionHandler(Exception ex) {
        log.error(ex.getMessage(), ex);
        return ResponseResult.failure("后台错误");
    }

    @ResponseBody
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public ResponseResult<String> httpRequestMethodNotSupportedExceptionHandle(HttpRequestMethodNotSupportedException hrmnse){
        log.error(hrmnse.getMessage(),hrmnse);
        return ResponseResult.failure("确认请求方式GET/POST");
    }

    @ResponseBody
    @ExceptionHandler(CheckIntegralException.class)
    public ResponseResult<String> checkIntegralException(CheckIntegralException cie){
        log.error(cie.getMessage(),cie);
        return ResponseResult.failure(ResponseResult.WRONG,"当前积分不足,无法执行退货操作");
    }

    @ResponseBody
    @ExceptionHandler(OrderShoesNullPointException.class)
    public ResponseResult<String> orderShoesNullPointException(OrderShoesNullPointException osnpe){
        log.error(osnpe.getMessage(),osnpe);
        return ResponseResult.failure(ResponseResult.WRONG,"订单商品列表为空,无法继续执行操作");
    }
}
